
program MakeStar

  ! generate a random list of stars, transformation and another list

  implicit none

  integer, parameter :: nstar = 10
  logical, parameter :: simple = .true.
  integer, dimension(nstar) :: id1,id2
  real, dimension(nstar) :: x1,y1,x2,y2
  integer :: i, j, k, id
  real :: x, y, x0, y0, A, B, c

  call random_seed()

  ! generate list
  do i = 1, nstar
     do 
        call random_number(x)
        call random_number(y)
        id1(i) = i
        x1(i) = x
        y1(i) = y
        j = 1
        do k = 1,i-1
           if( abs(x - x1(k)) < 0.1 .and. abs(y - y1(k)) < 0.1 ) j = 0
        enddo
        if( j > 0 ) exit
     enddo
  end do
  
  ! shake it
  id2 = id1
  do k = 1, 100
     call random_number(x)
     i = 1 + int(nstar*x)
     call random_number(y)
     j = 1 + int(nstar*y)
     id = id2(i)
     id2(i) = id2(j)
     id2(j) = id
  end do
     

  ! generate transformation
  call random_number(x)
  c = (x - 0.5)/2.5 + 1.0

  call random_number(x)
  x0 = 20.0*(x-0.5)
  call random_number(y)
  y0 = 20.0*(y-0.5)

  call random_number(x)
  A = x
  B = sqrt(1.0 - A**2)

  ! simple
  if( simple ) then
     c = 1.0
     x0 = 0.0
     y0 = 0.0
     A = 1.0
     B = 0.0
  end if

  ! generate another list
  do i = 1, nstar
     j = id2(i)
     x2(i) = c*(x0 + A*x1(j) - B*y1(j))
     y2(i) = c*(y0 + B*x1(j) + A*y1(j))
  end do

  write(*,*) '#',c, x0, y0, A, B
  do i = 1, nstar
     write(*,'(2(I5,2F12.7))') id1(i),x1(i),y1(i),id2(i),x2(i),y2(i)
!     write(*,'(2(2F12.7))') x1(i),y1(i),x2(i),y2(i)
  end do

end program MakeStar
